package controller

import (
	"cloudRestaurant/param"
	"cloudRestaurant/service"
	"cloudRestaurant/tool"
	"fmt"
	"github.com/gin-gonic/gin"
)

type MemberController struct {

}
//http://127.0.0.1:8090/api/sendcode?phone=13123123123
func (mc *MemberController) Router(engine *gin.Engine){
	engine.GET("/api/sendcode",mc.sendSmsCode)
	engine.OPTIONS("/api/login_sms",mc.SmsLogin)
}
// 发送验证码
func (mc *MemberController) sendSmsCode(Context *gin.Context){
	phone,exist := Context.GetQuery("phone")
	if !exist{
		tool.Failed(Context,"参数解析失败")
		return
	}
	fmt.Println(phone)
	ms := service.MemberService{}
	isSend := ms.SendCode(phone)
	if isSend{
		tool.Success(Context,"短信发送成功")
		return
	}else{
		tool.Failed(Context,"短信发送失败")
		return
	}
}
//手机号+短信登录
func (mc *MemberController)SmsLogin(Context *gin.Context){
	var smsLoginParam param.SmsLoginParam
	err:=tool.Decode(Context.Request.Body,&smsLoginParam)
	if err != nil{
		tool.Failed(Context,"参数解析失败")
		return
	}
	//完成登录+验证码
	us := service.MemberService{}
	member := us.SmsLogin(smsLoginParam)
	if member!=nil{
		tool.Success(Context,member)
		return
	}
	tool.Failed(Context,"登陆失败")

}


























